<template>
  <div>
    <h1>个人信息</h1>
    <el-form :model="Profile" ref="Profile">
      <el-upload
        class="avatar-uploader"
        :show-file-list="false"
        action="#"
        :auto-upload="false"
        :on-change="AvatarChange">
        <img v-if="Profile.avatar" :src="Profile.avatar" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>

      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="Profile.gender">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
          <el-radio label="其他"></el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="你最喜欢的一本书" prop="favbook">
        <el-input v-model="Profile.favBook"></el-input>
      </el-form-item>

      <el-form-item>
        <el-row type="flex" justify="space-around">
          <el-button plain @click="submitForm">提交</el-button>
        </el-row>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import { CustomGetProfile, CustomPostProfile } from '@/utils/request'
import { checkAuth } from '@/utils/checkAuth'

export default {
  name: 'cl-username-bound',
  data () {
    return {
      Profile: {
        userid: '',
        gender: '',
        favBook: '',
        avatar: ''
      }
    }
  },
  methods: {
    submitForm () {
      let self = this
      checkAuth().then(auth => {
        self.Profile.userid = auth.id
      })
      CustomPostProfile(this.Profile).then(rsp => {
        const { success, msg } = rsp.data
        if (success) {
          this.$notify({
            title: '提交成功'
          })
        } else {
          this.$notify({
            title: '提交失败',
            message: msg
          })
        }
      })
    },
    AvatarChange (file) {
      file = file.raw
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG / PNG 格式!')
      } else if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      } else {
        const reader = new FileReader()
        reader.readAsDataURL(file)
        reader.onload = () => {
          this.Profile.avatar = reader.result
        }
      }
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  },
  created () {
    CustomGetProfile().then(rsp => {
      if (rsp.data) {
        this.Profile = rsp.data.profile
      }
    })
  }
}
</script>

<style lang="scss" scoped>
.upload-demo {
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
